package com.perseygroup.apps;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.perseygroup.apps.module.edt.EdtGridActivity;
import com.perseygroup.apps.module.stages.ListeStagesActivity;
import com.perseygroup.apps.tools.c2dm.C2DMService;

public class MenuActivity extends GeneralActivity implements OnClickListener
{
	Button mAide, mEdt, mStages, mBonsplans, mPreferences, mAbout;
	TextView mSchoolName, mUserName;
	private final static int MENU_DECONNECTION = 1;
	private final static int MENU_QUITTER = 2;

	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        startService(new Intent(this,C2DMService.class));
        mAide = (Button) findViewById(R.id.aide);
        mEdt = (Button) findViewById(R.id.button_edt);
        mStages = (Button) findViewById(R.id.button_stages);
        mBonsplans = (Button) findViewById(R.id.button_bonsplans);
        mPreferences = (Button) findViewById(R.id.button_preferences);
        mAbout = (Button) findViewById(R.id.about);
        
        mSchoolName = (TextView) findViewById(R.id.schoolname);
        mUserName = (TextView) findViewById(R.id.username);
        String[] dataStudent = getStudentDataFromPrefs();
        if(dataStudent[1] != null)
        {
        	mSchoolName.setText(dataStudent[1].toUpperCase());
        }
        if(dataStudent[0] != null)
        {
        	mUserName.setText(dataStudent[0].toUpperCase());
        }
        
        mAide.setOnClickListener(this);
        mEdt.setOnClickListener(this);
        mStages.setOnClickListener(this);
        mBonsplans.setOnClickListener(this);
        mPreferences.setOnClickListener(this);
        mAbout.setOnClickListener(this);
        mPreferences.setOnClickListener(this);
    }
	
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		menu.add(0, MENU_DECONNECTION, Menu.NONE, R.string.text_deconnection).setIcon(R.drawable.deconnexion);
		menu.add(0, MENU_QUITTER, Menu.NONE, R.string.text_quitter).setIcon(R.drawable.quitter);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch (item.getItemId()) 
		{
			case MENU_DECONNECTION:
			{
				new AlertDialog.Builder(this)
				.setTitle(R.string.titre_deconnecter)
				.setMessage(R.string.confirmation_deconnection)
				.setCancelable(false)
				.setPositiveButton(R.string.text_oui, new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						//We remove the student data from prefs.
						try
						{
							removeStudentDataFromPrefs();
							removeCheckboxPrefs();
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
						setResult(RESULT_DECONNECTER, null);
						finish();
						//TODO Replace with the intent of authentication activity and erase preferences from memories
					}
				})
				.setNegativeButton(R.string.text_non, new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						
					}
				})
				.show();
			}
			break;
				
			case MENU_QUITTER:
			{
				new AlertDialog.Builder(this)
				.setTitle(R.string.titre_quitter)
				.setMessage(R.string.confirmation_quitter)
				.setCancelable(false)
				.setPositiveButton(R.string.text_oui, new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						FROM_MENU_QUITTER = true;
						setResult(RESULT_QUITTER, null);
						finish();
					}
				})
				.setNegativeButton(R.string.text_non, new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						
					}
				})
				.show();
			}
			break;
			
			default:
				break;
		}
		return true;
	}
	@Override
	public void onClick(View view)
	{	
	
		if(view == mStages)
		{
			startActivity(new Intent(this, ListeStagesActivity.class));
		}
		else if(view == mEdt)
		{
			startActivity(new Intent(this, EdtGridActivity.class));
			//startActivity(new Intent(this, com.android.calendar.CalendarActivity.class));
			/*Intent res = new Intent();
			String mPackage = "com.android.calendar";
			String mClass = ".LaunchActivity";
			res.setComponent(new ComponentName(mPackage,mPackage+mClass));
			startActivity(res);*/
			/*String className = "com.android.calendar.CalendarActivity";  
			Intent openNewIntent = new Intent( this, Class.forName( className ) );  
			startActivity(  openNewIntent );*/
		}
		else if (view == mAbout)
		{
			startActivity(new Intent(this, AproposActivity.class));
		}
		else if (view == mPreferences)
		{
			startActivity(new Intent(this, PreferencesActivity.class));
		}
	}
}